<?php
/**
 * Created by PhpStorm.
 * User: baiguiren
 * Date: 12/30/16
 * Time: 11:24 AM
 */

namespace App\Console;

use Illuminate\Console\Command;

//mysql每天备份, 需要在crontab -e添加以下命令, mysqldump和php使用绝对路径, 否则可能找不到命令
//* * * * * /usr/local/php/bin/php /wwwroot/fangsuyi/artisan schedule:run >> /dev/null 2>&1
class Sign extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sign';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '百度贴吧一键签到';


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->sign();
    }

    public function sign()
    {
        $receive_email_address = env('MAIL_TO_ADDRESS');
        set_time_limit(120);
        $bduss = "URvY0s1YWZ1d1BsfklwRmE5cGd0cmU0fjE5cks2cXlZMnp5S3VRVDJWM3VvS2xZSVFBQUFBJCQAAAAAAAAAAAEAAAD7oo0gYjkxNjgwOTQ5OAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO4TgljuE4JYZ";
        /*定义变量*/
        $tieba_header = array(
            'Content-Type: application/x-www-form-urlencoded',
            'Charset: UTF-8',
            'net: 3',
            'User-Agent: bdtb for Android 8.4.0.1',
            'Connection: Keep-Alive',
            'Accept-Encoding: gzip',
            'Host: c.tieba.baidu.com',
        );
        $firefox_header = array(
            'Host: tieba.baidu.com',
            'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0',
            'Accept: */*',
            'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
            'Referer: http://tieba.baidu.com/',
            'Connection: keep-alive',
        );
        $re=json_decode($this->xCurl('http://tieba.baidu.com/dc/common/tbs','BDUSS=' . $bduss,null,$firefox_header),true);
        if (! $re['is_login']) {
            \Mail::raw('登陆失败', function($message) use ($receive_email_address){
                $message->to($receive_email_address)->subject('定时任务出错');
            });
            $this->log('登陆失败');
        };
        $tbs = $re['tbs'];

        /*程序开始*/
        $postdata = array ('BDUSS='.$bduss,'tbs=' . $tbs);
        $postdata = implode('&', $postdata).'&sign='.md5(implode('', $postdata).'tiebaclient!!!');
        $re = json_decode(gzdecode($this->xCurl('http://c.tieba.baidu.com/c/c/forum/msign','ca=open',$postdata,$tieba_header)),true);
        if ($re['error_code'] == '0')
            $this->log('默认签成功，执行第二次签到。');
        else
            $this->log('默认签到出错，使用超级签到。');
        $re = json_decode($this->xCurl('http://tieba.baidu.com/tbmall/onekeySignin1','BDUSS='.$bduss,'ie=utf-8&tbs='.$tbs,$firefox_header),true);
        if (@$re['data']['unsignedForumAmount'] == '0'){
            $this->log('签到完成！已签' . @$re['data']['signedForumAmount'] . '个吧，' . @$re['data']['unsignedForumAmount'] . '个吧未签。');
            goto msign_end;
        }
        for ($pageno = 1; 1 ; $pageno ++){
            $postdata='BDUSS='.urlencode($bduss).'&_client_version=8.1.0.4'.'&page_no=' . $pageno.'&page_size=100'.'&sign='.md5('BDUSS='.$bduss.'_client_version=8.1.0.4'.'page_no='.$pageno.'page_size=100'.'tiebaclient!!!');
            $re = json_decode(gzdecode($this->xCurl('http://c.tieba.baidu.com/c/f/forum/like','ca=open',$postdata,$tieba_header)),true);
            foreach ($re['forum_list']['non-gconforum'] as $list) {
                $this->log('尝试签到“' . $list['name'].'吧”:');
                $re_o = json_decode(gzdecode($this->xCurl('http://c.tieba.baidu.com/c/c/forum/sign','ca=open','BDUSS='.urlencode($bduss).'&fid='.$list['id'].'&kw='.urlencode($list['name']).'&sign='.md5('BDUSS='.$bduss.'fid='.$list['id'].'kw='.$list['name'].'tbs='.$tbs.'tiebaclient!!!').'&tbs='.$tbs,$tieba_header)),true);
                if ($re_o['error_code'] == '0')
                    $this->log('签到完成，经验值加' . $re_o['user_info']['sign_bonus_point'] . '，你是今天第' . $re_o['user_info']['user_sign_rank'] . '个签到的。');
                else
                    $this->log($re_o['error_msg']);
            };
            if ($re['has_more'] == '0')
                break;
        };
        $re = json_decode($this->xCurl('http://tieba.baidu.com/tbmall/onekeySignin1','BDUSS='.$bduss,'ie=utf-8&tbs='.$tbs,$firefox_header),true);
        $this->log('签到完成！已签' . @$re['data']['signedForumAmount'] . '个吧，' . @$re['data']['unsignedForumAmount'] . '个吧未签。');
        msign_end:
        $this->log(date('Y年m月d日 H:i:s'));
    }

    public function log($message, $type=2) {
        if ($type == 1) {
            //输出
            echo $message . "\n";
        }
        if ($type == 2) {
            //写日志
            \Log::info($message);
        }
    }

    /*定义自定义函数*/
    function xCurl($url,$cookie=null,$postdata=null,$header=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        if (!is_null($postdata)) curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
        if (!is_null($cookie)) curl_setopt($ch, CURLOPT_COOKIE,$cookie);
        if (!empty($header)) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        $re = curl_exec($ch);
        curl_close($ch);
        return $re;
    }
}